package com.nativetalk.controller.app.three.share;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.website.TsNtWebSiteConfIndex;
import com.nativetalk.common.Constants;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.teacher.search.TeacherSearchService;
import com.nativetalk.service.three.member.search.ThreeMemberSearchService;
import com.nativetalk.service.three.ranking.RankingService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeShareController {
    private static Logger logger = Logger.getLogger(ThreeShareController.class);
    @Resource
    private TeacherSearchService teacherSearchService;
    @Resource
    private ThreeMemberSearchService threeMemberSearchService;
    @Resource
    private RankingService rankingService;

    /**
     * @Purpose  教师分享
     * @version  3.0
     * @time     2015-07-22
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value="/teacher/share" ,method = {RequestMethod.GET,RequestMethod.POST} )
    public ModelAndView register(@RequestParam(value = "user_id", required = false) Integer user_id,
                               HttpServletRequest request) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(user_id));

            ModelAndView mv  = new ModelAndView("/app/teacher/detail");
            Map<String, TsNtWebSiteConfIndex> tsNtWebSiteConfIndexMap = (Map<String, TsNtWebSiteConfIndex>) MemcachedUtils.get(MemcachedKey.tsNtWebSiteConfIndexMap);
            mv.addObject("computer_download_url",tsNtWebSiteConfIndexMap.get("31").getData_value());
            mv.addObject("iOS_download_url",tsNtWebSiteConfIndexMap.get("32").getData_value());
            mv.addObject("Android_download_url",tsNtWebSiteConfIndexMap.get("33").getData_value());
            mv.addObject("result",teacherSearchService.findTeacherInfo(user_id,null));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(mv));
            return mv;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return null;
        }
    }

    /**
     * @Purpose  打卡分享
     * @version  3.2
     * @time     2016-09-30
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value="/member/clockin" ,method = {RequestMethod.GET,RequestMethod.POST} )
    public ModelAndView clockin(@RequestParam(value = "id", required = false) Integer id,
                                 HttpServletRequest request) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(id));

            ModelAndView mv  = new ModelAndView("/app/clock/member");
            mv.addObject("result",threeMemberSearchService.findClockInData(id));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(mv));
            return mv;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return null;
        }
    }
    /**
     * @Purpose  排行榜
     * @version  3.2
     * @time     2016-09-30
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value="/teacher/ranking" ,method = {RequestMethod.GET,RequestMethod.POST} )
    public ModelAndView clockint(@RequestParam(value = "user_id", required = false) Integer user_id,
                                HttpServletRequest request) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(user_id));

            ModelAndView mv  = new ModelAndView("/app/ranking/teacher");
            mv.addObject("result",rankingService.findTeacherWeekRankingShare(user_id));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(mv));
            return mv;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return null;
        }
    }
    /**
     * @Purpose  排行榜
     * @version  3.2
     * @time     2016-09-30
     * @author   lizhun
     * @return   RetInfo
     */
    @RequestMapping(value="/member/ranking" ,method = {RequestMethod.GET,RequestMethod.POST} )
    public ModelAndView ranking(@RequestParam(value = "user_id", required = false) Integer user_id,
                                 HttpServletRequest request) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(user_id));

            ModelAndView mv  = new ModelAndView("/app/ranking/member");
            mv.addObject("result",rankingService.findWeekRankingShare(user_id));

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(mv));
            return mv;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return null;
        }
    }
}


